package site.anxuan.seckill.common;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;

import java.nio.charset.StandardCharsets;

public class FastJsonSerializer implements RedisSerializer<Object> {
    @Override
    public byte[] serialize(Object object) throws SerializationException {
        if (object == null) return null;
        String json = JSON.toJSONString(object, SerializerFeature.WriteClassName);
        return json.getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public Object deserialize(byte[] bytes) throws SerializationException {
        if (bytes == null || bytes.length == 0) return null;
        String json = new String(bytes, StandardCharsets.UTF_8);
        return JSON.parseObject(json, Object.class, Feature.SupportAutoType);
    }
}
